home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / include / X11 / Xos_r.h < prev    next >
C/C++ Source or Header  |  2006-04-12  |  36KB  |  1,158 lines

  1. /* $Xorg: Xos_r.h,v 1.4 2001/02/09 02:03:22 xorgcvs Exp $ */
  2. /*
  3. Copyright 1996, 1998  The Open Group
  4.  
  5. Permission to use, copy, modify, distribute, and sell this software and its
  6. documentation for any purpose is hereby granted without fee, provided that
  7. the above copyright notice appear in all copies and that both that
  8. copyright notice and this permission notice appear in supporting
  9. documentation.
  10.  
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13.  
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  17. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  18. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20.  
  21. Except as contained in this notice, the name of The Open Group shall not be
  22. used in advertising or otherwise to promote the sale, use or other dealings
  23. in this Software without prior written authorization from The Open Group.
  24. */
  25. /* $XFree86: xc/include/Xos_r.h,v 1.18tsi Exp $ */
  26.  
  27. /*
  28.  * Various and sundry Thread-Safe functions used by X11, Motif, and CDE.
  29.  *
  30.  * Use this file in MT-safe code where you would have included
  31.  *    <dirent.h>    for readdir()
  32.  *    <grp.h>        for getgrgid() or getgrnam()
  33.  *    <netdb.h>    for gethostbyname(), gethostbyaddr(), or getservbyname()
  34.  *    <pwd.h>        for getpwnam() or getpwuid()
  35.  *    <string.h>    for strtok()
  36.  *    <time.h>    for asctime(), ctime(), localtime(), or gmtime()
  37.  *    <unistd.h>    for getlogin() or ttyname()
  38.  * or their thread-safe analogs.
  39.  *
  40.  * If you are on a platform that defines XTHREADS but does not have
  41.  * MT-safe system API (e.g. UnixWare) you must define _Xos_processLock
  42.  * and _Xos_processUnlock macros before including this header.  If
  43.  * you are on OSF/1 V3.2 and plan to use readdir(), you must also define
  44.  * _Xos_isThreadsInitialized.  For convenience XOS_USE_XLIB_LOCKING or
  45.  * XOS_USE_XT_LOCKING may be defined to obtain either Xlib-only or
  46.  * Xt-based versions of these macros.  These macros won't result in
  47.  * truly thread-safe calls, but they are better than nothing.  If you
  48.  * do not want locking in this situation define XOS_USE_NO_LOCKING.
  49.  *
  50.  * NOTE: On systems lacking approriate _r functions Gethostbyname(),
  51.  *    Gethostbyaddr(), and Getservbyname() do NOT copy the host or
  52.  *    protocol lists!
  53.  *
  54.  * NOTE: On systems lacking appropriate _r functions Getgrgid() and
  55.  *    Getgrnam() do NOT copy the list of group members!
  56.  *
  57.  * This header is nominally intended to simplify porting X11, Motif, and
  58.  * CDE; it may be useful to other people too.  The structure below is
  59.  * complicated, mostly because P1003.1c (the IEEE POSIX Threads spec)
  60.  * went through lots of drafts, and some vendors shipped systems based
  61.  * on draft API that were changed later.  Unfortunately POSIX did not
  62.  * provide a feature-test macro for distinguishing each of the drafts.
  63.  */
  64.  
  65. /*
  66.  * This header has several parts.  Search for "Effective prototypes"
  67.  * to locate the beginning of a section.
  68.  */
  69.  
  70. /* This header can be included multiple times with different defines! */
  71. #ifndef _XOS_R_H_
  72. # define _XOS_R_H_
  73.  
  74. # include <X11/Xos.h>
  75. # include <X11/Xfuncs.h>
  76.  
  77. # ifndef X_NOT_POSIX
  78. #  ifdef _POSIX_SOURCE
  79. #   include <limits.h>
  80. #  else
  81. #   define _POSIX_SOURCE
  82. #   include <limits.h>
  83. #   undef _POSIX_SOURCE
  84. #  endif
  85. #  ifndef LINE_MAX
  86. #   define X_LINE_MAX 2048
  87. #  else
  88. #   define X_LINE_MAX LINE_MAX
  89. #  endif
  90. # endif
  91. #endif /* _XOS_R_H */
  92.  
  93. #ifndef WIN32
  94.  
  95. #ifdef __cplusplus
  96. extern "C" {
  97. #endif
  98.  
  99. # if defined(XOS_USE_XLIB_LOCKING)
  100. #  ifndef XAllocIDs /* Xlibint.h does not have multiple include protection */
  101. typedef struct _LockInfoRec *LockInfoPtr;
  102. extern LockInfoPtr _Xglobal_lock;
  103. #  endif
  104. #  ifndef _Xos_isThreadInitialized
  105. #   define _Xos_isThreadInitialized    (_Xglobal_lock)
  106. #  endif
  107. #  if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
  108. #   ifndef XAllocIDs /* Xlibint.h does not have multiple include protection */
  109. #    include <X11/Xfuncproto.h>    /* for NeedFunctionPrototypes */
  110. extern void (*_XLockMutex_fn)(
  111. #    if NeedFunctionPrototypes
  112.     LockInfoPtr    /* lock */, char * /* file */, int /* line */
  113. #    endif
  114. );
  115. extern void (*_XUnlockMutex_fn)(
  116. #    if NeedFunctionPrototypes
  117.     LockInfoPtr    /* lock */, char * /* file */, int /* line */
  118. #    endif
  119. );
  120. #   endif
  121. #   ifndef _Xos_processLock
  122. #    define _Xos_processLock    \
  123.   (_XLockMutex_fn ? (*_XLockMutex_fn)(_Xglobal_lock,__FILE__,__LINE__) : 0)
  124. #   endif
  125. #   ifndef _Xos_processUnlock
  126. #    define _Xos_processUnlock    \
  127.   (_XUnlockMutex_fn ? (*_XUnlockMutex_fn)(_Xglobal_lock,__FILE__,__LINE__) : 0)
  128. #   endif
  129. #  else
  130. #   ifndef XAllocIDs /* Xlibint.h does not have multiple include protection */
  131. #    include <X11/Xfuncproto.h>    /* for NeedFunctionPrototypes */
  132. extern void (*_XLockMutex_fn)(
  133. #    if NeedFunctionPrototypes
  134.     LockInfoPtr    /* lock */
  135. #    endif
  136. );
  137. extern void (*_XUnlockMutex_fn)(
  138. #    if NeedFunctionPrototypes
  139.     LockInfoPtr    /* lock */
  140. #    endif
  141. );
  142. #   endif
  143. #   ifndef _Xos_processLock
  144. #    define _Xos_processLock    \
  145.   (_XLockMutex_fn ? ((*_XLockMutex_fn)(_Xglobal_lock), 0) : 0)
  146. #   endif
  147. #   ifndef _Xos_processUnlock
  148. #    define _Xos_processUnlock    \
  149.   (_XUnlockMutex_fn ? ((*_XUnlockMutex_fn)(_Xglobal_lock), 0) : 0)
  150. #   endif
  151. #  endif
  152. # elif defined(XOS_USE_XT_LOCKING)
  153. #  ifndef _XtThreadsI_h
  154. extern void (*_XtProcessLock)(void);
  155. #  endif
  156. #  ifndef _XtintrinsicP_h
  157. #   include <X11/Xfuncproto.h>    /* for NeedFunctionPrototypes */
  158. extern void XtProcessLock(
  159. #   if NeedFunctionPrototypes
  160.     void
  161. #   endif
  162. );
  163. extern void XtProcessUnlock(
  164. #   if NeedFunctionPrototypes
  165.     void
  166. #   endif
  167. );
  168. #  endif
  169. #  ifndef _Xos_isThreadInitialized
  170. #   define _Xos_isThreadInitialized    _XtProcessLock
  171. #  endif
  172. #  ifndef _Xos_processLock
  173. #   define _Xos_processLock        XtProcessLock()
  174. #  endif
  175. #  ifndef _Xos_processUnlock
  176. #   define _Xos_processUnlock        XtProcessUnlock()
  177. #  endif
  178. # elif defined(XOS_USE_NO_LOCKING)
  179. #  ifndef _Xos_isThreadInitialized
  180. #   define _Xos_isThreadInitialized    0
  181. #  endif
  182. #  ifndef _Xos_processLock
  183. #   define _Xos_processLock        0
  184. #  endif
  185. #  ifndef _Xos_processUnlock
  186. #   define _Xos_processUnlock        0
  187. #  endif
  188. # endif
  189.  
  190. #endif /* !defined WIN32 */
  191.  
  192. /*
  193.  * Solaris 2.5 has SVR4 thread-safe API, but defines the POSIX
  194.  * thread-safe feature test macro.  Fix the feature test macro.
  195.  */
  196. #if defined(sun) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
  197. # undef _POSIX_THREAD_SAFE_FUNCTIONS
  198. #endif
  199.  
  200. /*
  201.  * LynxOS 3.1 defines _POSIX_THREAD_SAFE_FUNCTIONS but
  202.  * getpwuid_r has different semantics than defined by POSIX
  203.  */
  204. #if defined(Lynx) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
  205. # undef _POSIX_THREAD_SAFE_FUNCTIONS
  206. #endif
  207.  
  208.  
  209. /***** <pwd.h> wrappers *****/
  210.  
  211. /*
  212.  * Effective prototypes for <pwd.h> wrappers:
  213.  *
  214.  * #define X_INCLUDE_PWD_H
  215.  * #define XOS_USE_..._LOCKING
  216.  * #include <X11/Xos_r.h>
  217.  *
  218.  * typedef ... _Xgetpwparams;
  219.  *
  220.  * struct passwd* _XGetpwnam(const char *name, _Xgetpwparams);
  221.  * struct passwd* _XGetpwuid(uid_t uid, _Xgetpwparams);
  222.  */
  223.  
  224. #if defined(X_INCLUDE_PWD_H) && !defined(_XOS_INCLUDED_PWD_H)
  225. # include <pwd.h>
  226. # if defined(XUSE_MTSAFE_API) || defined(XUSE_MTSAFE_PWDAPI)
  227. #  define XOS_USE_MTSAFE_PWDAPI 1
  228. # endif
  229. #endif
  230.  
  231. #undef X_NEEDS_PWPARAMS
  232. #if !defined(X_INCLUDE_PWD_H) || defined(_XOS_INCLUDED_PWD_H)
  233. /* Do nothing */
  234.  
  235. #elif !defined(XTHREADS) && !defined(X_FORCE_USE_MTSAFE_API)
  236. /* Use regular, unsafe API. */
  237. # if defined(X_NOT_POSIX) && !defined(i386) && !defined(SYSV)
  238. extern struct passwd *getpwuid(), *getpwnam();
  239. # endif
  240. typedef int _Xgetpwparams;    /* dummy */
  241. # define _XGetpwuid(u,p)    getpwuid((u))
  242. # define _XGetpwnam(u,p)    getpwnam((u))
  243.  
  244. #elif !defined(XOS_USE_MTSAFE_PWDAPI) || defined(XNO_MTSAFE_PWDAPI)
  245. /* UnixWare 2.0, or other systems with thread support but no _r API. */
  246. # define X_NEEDS_PWPARAMS
  247. typedef struct {
  248.   struct passwd pws;
  249.   char   pwbuf[1024];
  250.   struct passwd* pwp;
  251.   size_t len;
  252. } _Xgetpwparams;
  253.  
  254. /*
  255.  * NetBSD and FreeBSD, at least, are missing several of the unixware passwd
  256.  * fields.
  257.  */
  258.  
  259. #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \
  260.     defined(__APPLE__)
  261. static __inline__ void _Xpw_copyPasswd(_Xgetpwparams p)
  262. {
  263.    memcpy(&(p).pws, (p).pwp, sizeof(struct passwd));
  264.  
  265.    (p).pws.pw_name = (p).pwbuf;
  266.    (p).len = strlen((p).pwp->pw_name);
  267.    strcpy((p).pws.pw_name, (p).pwp->pw_name);
  268.  
  269.    (p).pws.pw_passwd = (p).pws.pw_name + (p).len + 1;
  270.    (p).len = strlen((p).pwp->pw_passwd);
  271.    strcpy((p).pws.pw_passwd,(p).pwp->pw_passwd);
  272.  
  273.    (p).pws.pw_class = (p).pws.pw_passwd + (p).len + 1;
  274.    (p).len = strlen((p).pwp->pw_class);
  275.    strcpy((p).pws.pw_class, (p).pwp->pw_class);
  276.  
  277.    (p).pws.pw_gecos = (p).pws.pw_class + (p).len + 1;
  278.    (p).len = strlen((p).pwp->pw_gecos);
  279.    strcpy((p).pws.pw_gecos, (p).pwp->pw_gecos);
  280.  
  281.    (p).pws.pw_dir = (p).pws.pw_gecos + (p).len + 1;
  282.    (p).len = strlen((p).pwp->pw_dir);
  283.    strcpy((p).pws.pw_dir, (p).pwp->pw_dir);
  284.  
  285.    (p).pws.pw_shell = (p).pws.pw_dir + (p).len + 1;
  286.    (p).len = strlen((p).pwp->pw_shell);
  287.    strcpy((p).pws.pw_shell, (p).pwp->pw_shell);
  288.  
  289.    (p).pwp = &(p).pws;
  290. }
  291.  
  292. #else
  293. # define _Xpw_copyPasswd(p) \
  294.    (memcpy(&(p).pws, (p).pwp, sizeof(struct passwd)), \
  295.     ((p).pws.pw_name = (p).pwbuf), \
  296.     ((p).len = strlen((p).pwp->pw_name)), \
  297.     strcpy((p).pws.pw_name, (p).pwp->pw_name), \
  298.     ((p).pws.pw_passwd = (p).pws.pw_name + (p).len + 1), \
  299.     ((p).len = strlen((p).pwp->pw_passwd)), \
  300.     strcpy((p).pws.pw_passwd,(p).pwp->pw_passwd), \
  301.     ((p).pws.pw_age = (p).pws.pw_passwd + (p).len + 1), \
  302.     ((p).len = strlen((p).pwp->pw_age)), \
  303.     strcpy((p).pws.pw_age, (p).pwp->pw_age), \
  304.     ((p).pws.pw_comment = (p).pws.pw_age + (p).len + 1), \
  305.     ((p).len = strlen((p).pwp->pw_comment)), \
  306.     strcpy((p).pws.pw_comment, (p).pwp->pw_comment), \
  307.     ((p).pws.pw_gecos = (p).pws.pw_comment + (p).len + 1), \
  308.     ((p).len = strlen((p).pwp->pw_gecos)), \
  309.     strcpy((p).pws.pw_gecos, (p).pwp->pw_gecos), \
  310.     ((p).pws.pw_dir = (p).pws.pw_comment + (p).len + 1), \
  311.     ((p).len = strlen((p).pwp->pw_dir)), \
  312.     strcpy((p).pws.pw_dir, (p).pwp->pw_dir), \
  313.     ((p).pws.pw_shell = (p).pws.pw_dir + (p).len + 1), \
  314.     ((p).len = strlen((p).pwp->pw_shell)), \
  315.     strcpy((p).pws.pw_shell, (p).pwp->pw_shell), \
  316.     ((p).pwp = &(p).pws), \
  317.     0 )
  318. #endif
  319. # define _XGetpwuid(u,p) \
  320. ( (_Xos_processLock), \
  321.   (((p).pwp = getpwuid((u))) ? _Xpw_copyPasswd(p), 0 : 0), \
  322.   (_Xos_processUnlock), \
  323.   (p).pwp )
  324. # define _XGetpwnam(u,p) \
  325. ( (_Xos_processLock), \
  326.   (((p).pwp = getpwnam((u))) ? _Xpw_copyPasswd(p), 0 : 0), \
  327.   (_Xos_processUnlock), \
  328.   (p).pwp )
  329.  
  330. #elif !defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(__APPLE__)
  331. /* SVR4 threads, AIX 4.2.0 and earlier and OSF/1 3.2 and earlier pthreads */
  332. # define X_NEEDS_PWPARAMS
  333. typedef struct {
  334.   struct passwd pws;
  335.   char pwbuf[X_LINE_MAX];
  336. } _Xgetpwparams;
  337. # if defined(_POSIX_REENTRANT_FUNCTIONS) || !defined(SVR4) || defined(Lynx)
  338. #  ifndef Lynx
  339. #   define _XGetpwuid(u,p) \
  340. ((getpwuid_r((u),&(p).pws,(p).pwbuf,sizeof((p).pwbuf)) == -1) ? NULL : &(p).pws)
  341. #   define _XGetpwnam(u,p) \
  342. ((getpwnam_r((u),&(p).pws,(p).pwbuf,sizeof((p).pwbuf)) == -1) ? NULL : &(p).pws)
  343. #  else /* Lynx */
  344. #   define _XGetpwuid(u,p) \
  345. ((getpwuid_r(&(p).pws,(u),(p).pwbuf,sizeof((p).pwbuf)) == -1) ? NULL : &(p).pws)
  346. #   define _XGetpwnam(u,p) \
  347. ((getpwnam_r(&(p).pws,(u),(p).pwbuf,sizeof((p).pwbuf)) == -1) ? NULL : &(p).pws)
  348. #  endif
  349. # else /* SVR4 */
  350. #  define _XGetpwuid(u,p) \
  351. ((getpwuid_r((u),&(p).pws,(p).pwbuf,sizeof((p).pwbuf)) == NULL) ? NULL : &(p).pws)
  352. #  define _XGetpwnam(u,p) \
  353. ((getpwnam_r((u),&(p).pws,(p).pwbuf,sizeof((p).pwbuf)) == NULL) ? NULL : &(p).pws)
  354. # endif /* SVR4 */
  355.  
  356. #else /* _POSIX_THREAD_SAFE_FUNCTIONS */
  357. /* Digital UNIX 4.0, but not (beta) T4.0-1 */
  358. # if defined(__osf__)
  359. /* OSF/1 V4.0 <pwd.h> doesn't declare the _P routines, breaking under C++. */
  360. extern int _Pgetpwuid_r(uid_t, struct passwd *, char *, size_t, struct passwd **);
  361. extern int _Pgetpwnam_r(const char *, struct passwd *, char *, size_t, struct passwd **);
  362. # endif
  363. # define X_NEEDS_PWPARAMS
  364. typedef struct {
  365.   struct passwd pws;
  366.   char pwbuf[X_LINE_MAX];
  367.   struct passwd* pwp;
  368. } _Xgetpwparams;
  369. typedef int _Xgetpwret;
  370. # define _XGetpwuid(u,p) \
  371. ((getpwuid_r((u),&(p).pws,(p).pwbuf,sizeof((p).pwbuf),&(p).pwp) == -1) ? \
  372.  NULL : (p).pwp)
  373. # define _XGetpwnam(u,p) \
  374. ((getpwnam_r((u),&(p).pws,(p).pwbuf,sizeof((p).pwbuf),&(p).pwp) == -1) ? \
  375.  NULL : (p).pwp)
  376. #endif /* X_INCLUDE_PWD_H */
  377.  
  378. #if defined(X_INCLUDE_PWD_H) && !defined(_XOS_INCLUDED_PWD_H)
  379. # define _XOS_INCLUDED_PWD_H
  380. #endif
  381.  
  382.  
  383. /***** <netdb.h> wrappers *****/
  384.  
  385. /*
  386.  * Effective prototypes for <netdb.h> wrappers:
  387.  *
  388.  * NOTE: On systems lacking the appropriate _r functions Gethostbyname(),
  389.  *    Gethostbyaddr(), and Getservbyname() do NOT copy the host or
  390.  *    protocol lists!
  391.  *
  392.  * #define X_INCLUDE_NETDB_H
  393.  * #define XOS_USE_..._LOCKING
  394.  * #include <X11/Xos_r.h>
  395.  *
  396.  * typedef ... _Xgethostbynameparams;
  397.  * typedef ... _Xgetservbynameparams;
  398.  *
  399.  * struct hostent* _XGethostbyname(const char* name,_Xgethostbynameparams);
  400.  * struct hostent* _XGethostbyaddr(const char* addr, int len, int type,
  401.  *                   _Xgethostbynameparams);
  402.  * struct servent* _XGetservbyname(const char* name, const char* proto,
  403.  *                 _Xgetservbynameparams);
  404.  */
  405.  
  406. #undef XTHREADS_NEEDS_BYNAMEPARAMS
  407. #if defined(X_INCLUDE_NETDB_H) && !defined(_XOS_INCLUDED_NETDB_H) \
  408.     && !defined(WIN32)
  409. # include <netdb.h>
  410. # if defined(XUSE_MTSAFE_API) || defined(XUSE_MTSAFE_NETDBAPI)
  411. #  define XOS_USE_MTSAFE_NETDBAPI 1
  412. # endif
  413. #endif
  414.  
  415. #if !defined(X_INCLUDE_NETDB_H) || defined(_XOS_INCLUDED_NETDB_H)
  416. /* Do nothing. */
  417.  
  418. #elif !defined(XTHREADS) && !defined(X_FORCE_USE_MTSAFE_API)
  419. /* Use regular, unsafe API. */
  420. typedef int _Xgethostbynameparams; /* dummy */
  421. typedef int _Xgetservbynameparams; /* dummy */
  422. # define _XGethostbyname(h,hp)        gethostbyname((h))
  423. # define _XGethostbyaddr(a,al,t,hp)    gethostbyaddr((a),(al),(t))
  424. # define _XGetservbyname(s,p,sp)    getservbyname((s),(p))
  425.  
  426. #elif !defined(XOS_USE_MTSAFE_NETDBAPI) || defined(XNO_MTSAFE_NETDBAPI)
  427. /* UnixWare 2.0, or other systems with thread support but no _r API. */
  428. /* WARNING:  The h_addr_list and s_aliases values are *not* copied! */
  429.  
  430. #if defined(__NetBSD__) || defined(__FreeBSD__)
  431. #include <sys/param.h>
  432. #endif
  433.  
  434. typedef struct {
  435.   struct hostent hent;
  436.   char           h_name[MAXHOSTNAMELEN];
  437.   struct hostent *hptr;
  438. } _Xgethostbynameparams;
  439. typedef struct {
  440.   struct servent sent;
  441.   char           s_name[255];
  442.   char         s_proto[255];
  443.   struct servent *sptr;
  444. } _Xgetservbynameparams;
  445.  
  446. # define XTHREADS_NEEDS_BYNAMEPARAMS
  447.  
  448. # define _Xg_copyHostent(hp) \
  449.    (memcpy(&(hp).hent, (hp).hptr, sizeof(struct hostent)), \
  450.     strcpy((hp).h_name, (hp).hptr->h_name), \
  451.     ((hp).hent.h_name = (hp).h_name), \
  452.     ((hp).hptr = &(hp).hent), \
  453.      0 )
  454. # define _Xg_copyServent(sp) \
  455.    (memcpy(&(sp).sent, (sp).sptr, sizeof(struct servent)), \
  456.     strcpy((sp).s_name, (sp).sptr->s_name), \
  457.     ((sp).sent.s_name = (sp).s_name), \
  458.     strcpy((sp).s_proto, (sp).sptr->s_proto), \
  459.     ((sp).sent.s_proto = (sp).s_proto), \
  460.     ((sp).sptr = &(sp).sent), \
  461.     0 )
  462. # define _XGethostbyname(h,hp) \
  463.    ((_Xos_processLock), \
  464.     (((hp).hptr = gethostbyname((h))) ? _Xg_copyHostent(hp) : 0), \
  465.     (_Xos_processUnlock), \
  466.     (hp).hptr )
  467. # define _XGethostbyaddr(a,al,t,hp) \
  468.    ((_Xos_processLock), \
  469.     (((hp).hptr = gethostbyaddr((a),(al),(t))) ? _Xg_copyHostent(hp) : 0), \
  470.     (_Xos_processUnlock), \
  471.     (hp).hptr )
  472. # define _XGetservbyname(s,p,sp) \
  473.    ((_Xos_processLock), \
  474.     (((sp).sptr = getservbyname((s),(p))) ? _Xg_copyServent(sp) : 0), \
  475.     (_Xos_processUnlock), \
  476.     (sp).sptr )
  477.  
  478. #elif defined(XUSE_NETDB_R_API)
  479. /*
  480.  * POSIX does not specify _r equivalents for <netdb.h> API, but some
  481.  * vendors provide them anyway.  Use them only when explicitly asked.
  482.  */
  483. # ifdef _POSIX_REENTRANT_FUNCTIONS
  484. #  ifndef _POSIX_THREAD_SAFE_FUNCTIONS
  485. #   if defined(AIXV3) || defined(AIXV4) || defined(__osf__)
  486. #    define X_POSIX_THREAD_SAFE_FUNCTIONS 1
  487. #   endif
  488. #  endif
  489. # endif
  490. # ifdef _POSIX_THREAD_SAFE_FUNCTIONS
  491. #  define X_POSIX_THREAD_SAFE_FUNCTIONS 1
  492. # endif
  493.  
  494. # define XTHREADS_NEEDS_BYNAMEPARAMS
  495.  
  496. # ifndef X_POSIX_THREAD_SAFE_FUNCTIONS
  497. typedef struct {
  498.     struct hostent      hent;
  499.     char                hbuf[X_LINE_MAX];
  500.     int                 herr;
  501. } _Xgethostbynameparams;
  502. typedef struct {
  503.     struct servent      sent;
  504.     char                sbuf[X_LINE_MAX];
  505. } _Xgetservbynameparams;
  506. #  define _XGethostbyname(h,hp) \
  507.   gethostbyname_r((h),&(hp).hent,(hp).hbuf,sizeof((hp).hbuf),&(hp).herr)
  508. #  define _XGethostbyaddr(a,al,t,hp) \
  509.   gethostbyaddr_r((a),(al),(t),&(hp).hent,(hp).hbuf,sizeof((hp).hbuf),&(hp).herr)
  510. #  define _XGetservbyname(s,p,sp) \
  511.   getservbyname_r((s),(p),&(sp).sent,(sp).sbuf,sizeof((sp).sbuf))
  512. # else
  513. typedef struct {
  514.   struct hostent      hent;
  515.   struct hostent_data hdata;
  516. } _Xgethostbynameparams;
  517. typedef struct {
  518.   struct servent      sent;
  519.   struct servent_data sdata;
  520. } _Xgetservbynameparams;
  521. #  define _XGethostbyname(h,hp) \
  522.   (bzero((char*)&(hp).hdata,sizeof((hp).hdata)),    \
  523.    ((gethostbyname_r((h),&(hp).hent,&(hp).hdata) == -1) ? NULL : &(hp).hent))
  524. #  define _XGethostbyaddr(a,al,t,hp) \
  525.   (bzero((char*)&(hp).hdata,sizeof((hp).hdata)),    \
  526.    ((gethostbyaddr_r((a),(al),(t),&(hp).hent,&(hp).hdata) == -1) ? NULL : &(hp).hent))
  527. #  define _XGetservbyname(s,p,sp) \
  528.   (bzero((char*)&(sp).sdata,sizeof((sp).sdata)),    \
  529.    ((getservbyname_r((s),(p),&(sp).sent,&(sp).sdata) == -1) ? NULL : &(sp).sent) )
  530. # endif
  531. # ifdef X_POSIX_THREAD_SAFE_FUNCTIONS
  532. #  undef X_POSIX_THREAD_SAFE_FUNCTIONS
  533. # endif
  534.  
  535. #else
  536. /* The regular API is assumed to be MT-safe under POSIX. */
  537. typedef int _Xgethostbynameparams; /* dummy */
  538. typedef int _Xgetservbynameparams; /* dummy */
  539. # define _XGethostbyname(h,hp)        gethostbyname((h))
  540. # define _XGethostbyaddr(a,al,t,hp)    gethostbyaddr((a),(al),(t))
  541. # define _XGetservbyname(s,p,sp)    getservbyname((s),(p))
  542. #endif /* X_INCLUDE_NETDB_H */
  543.  
  544. #if defined(X_INCLUDE_NETDB_H) && !defined(_XOS_INCLUDED_NETDB_H)
  545. # define _XOS_INCLUDED_NETDB_H
  546. #endif
  547.  
  548.  
  549. /***** <dirent.h> wrappers *****/
  550.  
  551. /*
  552.  * Effective prototypes for <dirent.h> wrappers:
  553.  *
  554.  * #define X_INCLUDE_DIRENT_H
  555.  * #define XOS_USE_..._LOCKING
  556.  * #include <X11/Xos_r.h>
  557.  *
  558.  * typedef ... _Xreaddirparams;
  559.  *
  560.  * struct dirent *_XReaddir(DIR *dir_pointer, _Xreaddirparams);
  561.  */
  562.  
  563. #if defined(X_INCLUDE_DIRENT_H) && !defined(_XOS_INCLUDED_DIRENT_H)
  564. # include <sys/types.h>
  565. # if !defined(X_NOT_POSIX) || defined(SYSV) || defined(USG)
  566. #  include <dirent.h>
  567. # else
  568. #  include <sys/dir.h>
  569. #  ifndef dirent
  570. #   define dirent direct
  571. #  endif
  572. # endif
  573. # if defined(XUSE_MTSAFE_API) || defined(XUSE_MTSAFE_DIRENTAPI)
  574. #  define XOS_USE_MTSAFE_DIRENTAPI 1
  575. # endif
  576. #endif
  577.  
  578. #if !defined(X_INCLUDE_DIRENT_H) || defined(_XOS_INCLUDED_DIRENT_H)
  579. /* Do nothing. */
  580.  
  581. #elif !defined(XTHREADS) && !defined(X_FORCE_USE_MTSAFE_API)
  582. /* Use regular, unsafe API. */
  583. typedef int _Xreaddirparams;    /* dummy */
  584. # define _XReaddir(d,p)    readdir(d)
  585.  
  586. #elif !defined(XOS_USE_MTSAFE_DIRENTAPI) || defined(XNO_MTSAFE_DIRENTAPI)
  587. /* Systems with thread support but no _r API. */
  588. typedef struct {
  589.   struct dirent *result;
  590.   struct dirent dir_entry;
  591. # ifdef _POSIX_PATH_MAX
  592.   char buf[_POSIX_PATH_MAX];
  593. # elif defined(NAME_MAX)
  594.   char buf[NAME_MAX];
  595. # else
  596.   char buf[255];
  597. # endif
  598. } _Xreaddirparams;
  599.  
  600. # define _XReaddir(d,p)    \
  601.  ( (_Xos_processLock),                         \
  602.    (((p).result = readdir((d))) ?                 \
  603.     (memcpy(&((p).dir_entry), (p).result, (p).result->d_reclen), \
  604.      ((p).result = &(p).dir_entry), 0) :             \
  605.     0),                                 \
  606.    (_Xos_processUnlock),                     \
  607.    (p).result )
  608.  
  609. #else
  610. typedef struct {
  611.   struct dirent *result;
  612.   struct dirent dir_entry;
  613. # ifdef _POSIX_PATH_MAX
  614.   char buf[_POSIX_PATH_MAX];
  615. # elif defined(NAME_MAX)
  616.   char buf[NAME_MAX];
  617. # else
  618.   char buf[255];
  619. # endif
  620. } _Xreaddirparams;
  621.  
  622. # if defined(_POSIX_THREAD_SAFE_FUNCTIONS) || defined(AIXV3) || \
  623.      defined(AIXV4) || defined(__APPLE__)
  624. /* AIX defines the draft POSIX symbol, but uses the final API. */
  625. /* POSIX final API, returns (int)0 on success. */
  626. #  if defined(__osf__)
  627. /* OSF/1 V4.0 <dirent.h> doesn't declare _Preaddir_r, breaking under C++. */
  628. extern int _Preaddir_r(DIR *, struct dirent *, struct dirent **);
  629. #  endif
  630. #  define _XReaddir(d,p)                        \
  631.     (readdir_r((d), &((p).dir_entry), &((p).result)) ? NULL : (p).result)
  632. # elif defined(_POSIX_REENTRANT_FUNCTIONS) && defined(__osf__)
  633. /*
  634.  * OSF/1 V3.2 readdir_r() will SEGV if the main program is not
  635.  * explicitly linked with -lc_r.  The library REQUIREDLIBS don't help.
  636.  * Assume that if threads have been initialized we're linked properly.
  637.  */
  638. #  define _XReaddir(d,p)                        \
  639.  ( (_Xos_isThreadInitialized) ?                        \
  640.    (readdir_r((d), &((p).dir_entry)) ? NULL : &((p).dir_entry)) :    \
  641.    ((_Xos_processLock),                            \
  642.     (((p).result = readdir((d))) ?                    \
  643.      (memcpy(&((p).dir_entry), (p).result, (p).result->d_reclen),    \
  644.       ((p).result = &(p).dir_entry), 0) :                \
  645.      0),                                \
  646.     (_Xos_processUnlock),                        \
  647.     (p).result) )
  648. # elif defined(_POSIX_REENTRANT_FUNCTIONS)
  649. /* POSIX draft API, returns (int)0 on success. */
  650. #  define _XReaddir(d,p)    \
  651.     (readdir_r((d),&((p).dir_entry)) ? NULL : &((p).dir_entry))
  652. # elif defined(SVR4)
  653. /* Pre-POSIX API, returns non-NULL on success. */
  654. #  define _XReaddir(d,p)    (readdir_r((d), &(p).dir_entry))
  655. # else
  656. /* We have no idea what is going on.  Fake it all using process locks. */
  657. #  define _XReaddir(d,p)    \
  658.     ( (_Xos_processLock),                        \
  659.       (((p).result = readdir((d))) ?                    \
  660.        (memcpy(&((p).dir_entry), (p).result, (p).result->d_reclen),    \
  661.     ((p).result = &(p).dir_entry), 0) :                \
  662.        0),                                \
  663.       (_Xos_processUnlock),                        \
  664.       (p).result )
  665. # endif
  666. #endif /* X_INCLUDE_DIRENT_H */
  667.  
  668. #if defined(X_INCLUDE_DIRENT_H) && !defined(_XOS_INCLUDED_DIRENT_H)
  669. # define _XOS_INCLUDED_DIRENT_H
  670. #endif
  671.  
  672.  
  673. /***** <unistd.h> wrappers *****/
  674.  
  675. /*
  676.  * Effective prototypes for <unistd.h> wrappers:
  677.  *
  678.  * #define X_INCLUDE_UNISTD_H
  679.  * #define XOS_USE_..._LOCKING
  680.  * #include <X11/Xos_r.h>
  681.  *
  682.  * typedef ... _Xgetloginparams;
  683.  * typedef ... _Xttynameparams;
  684.  *
  685.  * char *_XGetlogin(_Xgetloginparams);
  686.  * char *_XTtyname(int, _Xttynameparams);
  687.  */
  688.  
  689. #if defined(X_INCLUDE_UNISTD_H) && !defined(_XOS_INCLUDED_UNISTD_H)
  690. /* <unistd.h> already included by <X11/Xos.h> */
  691. # if defined(XUSE_MTSAFE_API) || defined(XUSE_MTSAFE_UNISTDAPI)
  692. #  define XOS_USE_MTSAFE_UNISTDAPI 1
  693. # endif
  694. #endif
  695.  
  696. #if !defined(X_INCLUDE_UNISTD_H) || defined(_XOS_INCLUDED_UNISTD_H)
  697. /* Do nothing. */
  698.  
  699. #elif !defined(XTHREADS) && !defined(X_FORCE_USE_MTSAFE_API)
  700. /* Use regular, unsafe API. */
  701. typedef int _Xgetloginparams;    /* dummy */
  702. typedef int _Xttynameparams;    /* dummy */
  703. # define _XGetlogin(p)    getlogin()
  704. # define _XTtyname(f)    ttyname((f))
  705.  
  706. #elif !defined(XOS_USE_MTSAFE_UNISTDAPI) || defined(XNO_MTSAFE_UNISTDAPI)
  707. /* Systems with thread support but no _r API. */
  708. typedef struct {
  709.   char *result;
  710. # if defined(MAXLOGNAME)
  711.   char buf[MAXLOGNAME];
  712. # elif defined(LOGIN_NAME_MAX)
  713.   char buf[LOGIN_NAME_MAX];
  714. # else
  715.   char buf[64];
  716. # endif
  717. } _Xgetloginparams;
  718. typedef struct {
  719.   char *result;
  720. # ifdef TTY_NAME_MAX
  721.   char buf[TTY_NAME_MAX];
  722. # elif defined(_POSIX_TTY_NAME_MAX)
  723.   char buf[_POSIX_TTY_NAME_MAX];
  724. # elif defined(_POSIX_PATH_MAX)
  725.   char buf[_POSIX_PATH_MAX];
  726. # else
  727.   char buf[256];
  728. # endif
  729. } _Xttynameparams;
  730.  
  731. # define _XGetlogin(p) \
  732.  ( (_Xos_processLock), \
  733.    (((p).result = getlogin()) ? \
  734.     (strncpy((p).buf, (p).result, sizeof((p).buf)), \
  735.      ((p).buf[sizeof((p).buf)-1] = '\0'), \
  736.      ((p).result = (p).buf), 0) : 0), \
  737.    (_Xos_processUnlock), \
  738.    (p).result )
  739. #define _XTtyname(f,p) \
  740.  ( (_Xos_processLock), \
  741.    (((p).result = ttyname(f)) ? \
  742.     (strncpy((p).buf, (p).result, sizeof((p).buf)), \
  743.      ((p).buf[sizeof((p).buf)-1] = '\0'), \
  744.      ((p).result = (p).buf), 0) : 0), \
  745.    (_Xos_processUnlock), \
  746.    (p).result )
  747.  
  748. #elif defined(_POSIX_THREAD_SAFE_FUNCTIONS) || defined(_POSIX_REENTRANT_FUNCTIONS)
  749. /* POSIX API.
  750.  *
  751.  * extern int getlogin_r(char *, size_t);
  752.  * extern int ttyname_r(int, char *, size_t);
  753.  */
  754. typedef struct {
  755. # if defined(MAXLOGNAME)
  756.   char buf[MAXLOGNAME];
  757. # elif defined(LOGIN_NAME_MAX)
  758.   char buf[LOGIN_NAME_MAX];
  759. # else
  760.   char buf[64];
  761. # endif
  762. } _Xgetloginparams;
  763. typedef struct {
  764. # ifdef TTY_NAME_MAX
  765.   char buf[TTY_NAME_MAX];
  766. # elif defined(_POSIX_TTY_NAME_MAX)
  767.   char buf[_POSIX_TTY_NAME_MAX];
  768. # elif defined(_POSIX_PATH_MAX)
  769.   char buf[_POSIX_PATH_MAX];
  770. # else
  771.   char buf[256];
  772. # endif
  773. } _Xttynameparams;
  774.  
  775. # define _XGetlogin(p)    (getlogin_r((p).buf, sizeof((p).buf)) ? NULL : (p).buf)
  776. # define _XTtyname(f,p)    \
  777.     (ttyname_r((f), (p).buf, sizeof((p).buf)) ? NULL : (p).buf)
  778.  
  779. #else
  780. /* Pre-POSIX API.
  781.  *
  782.  * extern char *getlogin_r(char *, size_t);
  783.  * extern char *ttyname_r(int, char *, size_t);
  784.  */
  785. typedef struct {
  786. # if defined(MAXLOGNAME)
  787.   char buf[MAXLOGNAME];
  788. # elif defined(LOGIN_NAME_MAX)
  789.   char buf[LOGIN_NAME_MAX];
  790. # else
  791.   char buf[64];
  792. # endif
  793. } _Xgetloginparams;
  794. typedef struct {
  795. # ifdef TTY_NAME_MAX
  796.   char buf[TTY_NAME_MAX];
  797. # elif defined(_POSIX_TTY_NAME_MAX)
  798.   char buf[_POSIX_TTY_NAME_MAX];
  799. # elif defined(_POSIX_PATH_MAX)
  800.   char buf[_POSIX_PATH_MAX];
  801. # else
  802.   char buf[256];
  803. # endif
  804. } _Xttynameparams;
  805.  
  806. # define _XGetlogin(p)    getlogin_r((p).buf, sizeof((p).buf))
  807. # define _XTtyname(f,p)    ttyname_r((f), (p).buf, sizeof((p).buf))
  808. #endif /* X_INCLUDE_UNISTD_H */
  809.  
  810. #if defined(X_INCLUDE_UNISTD_H) && !defined(_XOS_INCLUDED_UNISTD_H)
  811. # define _XOS_INCLUDED_UNISTD_H
  812. #endif
  813.  
  814.  
  815. /***** <string.h> wrappers *****/
  816.  
  817. /*
  818.  * Effective prototypes for <string.h> wrappers:
  819.  *
  820.  * #define X_INCLUDE_STRING_H
  821.  * #define XOS_USE_..._LOCKING
  822.  * #include <X11/Xos_r.h>
  823.  *
  824.  * typedef ... _Xstrtokparams;
  825.  *
  826.  * char *_XStrtok(char *, const char*, _Xstrtokparams);
  827.  */
  828.  
  829. #if defined(X_INCLUDE_STRING_H) && !defined(_XOS_INCLUDED_STRING_H)
  830. /* <string.h> has already been included by <X11/Xos.h> */
  831. # if defined(XUSE_MTSAFE_API) || defined(XUSE_MTSAFE_STRINGAPI)
  832. #  define XOS_USE_MTSAFE_STRINGAPI 1
  833. # endif
  834. #endif
  835.  
  836. #if !defined(X_INCLUDE_STRING_H) || defined(_XOS_INCLUDED_STRING_H)
  837. /* Do nothing. */
  838.  
  839. #elif !defined(XTHREADS) && !defined(X_FORCE_USE_MTSAFE_API)
  840. /* Use regular, unsafe API. */
  841. typedef int _Xstrtokparams;    /* dummy */
  842. # define _XStrtok(s1,s2,p) \
  843.  ( p = 0, (void)p, strtok((s1),(s2)) )
  844.  
  845. #elif !defined(XOS_USE_MTSAFE_STRINGAPI) || defined(XNO_MTSAFE_STRINGAPI)
  846. /* Systems with thread support but no _r API. */
  847. typedef char *_Xstrtokparams;
  848. # define _XStrtok(s1,s2,p) \
  849.  ( (_Xos_processLock), \
  850.    ((p) = strtok((s1),(s2))), \
  851.    (_Xos_processUnlock), \
  852.    (p) )
  853.  
  854. #else
  855. /* POSIX or pre-POSIX API. */
  856. typedef char * _Xstrtokparams;
  857. # define _XStrtok(s1,s2,p)    strtok_r((s1),(s2),&(p))
  858. #endif /* X_INCLUDE_STRING_H */
  859.  
  860.  
  861. /***** <time.h> wrappers *****/
  862.  
  863. /*
  864.  * Effective prototypes for <time.h> wrappers:
  865.  *
  866.  * #define X_INCLUDE_TIME_H
  867.  * #define XOS_USE_..._LOCKING
  868.  * #include <X11/Xos_r.h>
  869.  *
  870.  * typedef ... _Xatimeparams;
  871.  * typedef ... _Xctimeparams;
  872.  * typedef ... _Xgtimeparams;
  873.  * typedef ... _Xltimeparams;
  874.  *
  875.  * char *_XAsctime(const struct tm *, _Xatimeparams);
  876.  * char *_XCtime(const time_t *, _Xctimeparams);
  877.  * struct tm *_XGmtime(const time_t *, _Xgtimeparams);
  878.  * struct tm *_XLocaltime(const time_t *, _Xltimeparams);
  879.  */
  880.  
  881. #if defined(X_INCLUDE_TIME_H) && !defined(_XOS_INCLUDED_TIME_H)
  882. # include <time.h>
  883. # if defined(XUSE_MTSAFE_API) || defined(XUSE_MTSAFE_TIMEAPI)
  884. #  define XOS_USE_MTSAFE_TIMEAPI 1
  885. # endif
  886. #endif
  887.  
  888. #if !defined(X_INCLUDE_TIME_H) || defined(_XOS_INCLUDED_TIME_H)
  889. /* Do nothing. */
  890.  
  891. #elif !defined(XTHREADS) && !defined(X_FORCE_USE_MTSAFE_API)
  892. /* Use regular, unsafe API. */
  893. typedef int _Xatimeparams;    /* dummy */
  894. # define _XAsctime(t,p)        asctime((t))
  895. typedef int _Xctimeparams;    /* dummy */
  896. # define _XCtime(t,p)        ctime((t))
  897. typedef int _Xgtimeparams;    /* dummy */
  898. # define _XGmtime(t,p)        gmtime((t))
  899. typedef int _Xltimeparams;    /* dummy */
  900. # define _XLocaltime(t,p)    localtime((t))
  901.  
  902. #elif !defined(XOS_USE_MTSAFE_TIMEAPI) || defined(XNO_MTSAFE_TIMEAPI)
  903. /* Systems with thread support but no _r API. */
  904. typedef struct {
  905. # ifdef TIMELEN
  906.   char buf[TIMELEN];
  907. # else
  908.   char buf[26];
  909. # endif
  910.   char *result;
  911. } _Xctimeparams, _Xatimeparams;
  912. typedef struct {
  913.   struct tm buf;
  914.   struct tm *result;
  915. } _Xgtimeparams, _Xltimeparams;
  916. # define _XAsctime(t,p) \
  917.  ( (_Xos_processLock), \
  918.    (((p).result = asctime((t))) ? \
  919.     (strncpy((p).buf, (p).result, sizeof((p).buf)), (p).result = &(p).buf) : \
  920.     0), \
  921.    (_Xos_processUnlock), \
  922.    (p).result )
  923. # define _XCtime(t,p) \
  924.  ( (_Xos_processLock), \
  925.    (((p).result = ctime((t))) ? \
  926.     (strncpy((p).buf, (p).result, sizeof((p).buf)), (p).result = &(p).buf) : \
  927.     0), \
  928.    (_Xos_processUnlock), \
  929.    (p).result )
  930. # define _XGmtime(t,p) \
  931.  ( (_Xos_processLock), \
  932.    (((p).result = gmtime(t)) ? \
  933.     (memcpy(&(p).buf, (p).result, sizeof((p).buf)), (p).result = &(p).buf) : \
  934.     0), \
  935.    (_Xos_processUnlock), \
  936.    (p).result )
  937. # define _XLocaltime(t,p) \
  938.  ( (_Xos_processLock), \
  939.    (((p).result = localtime(t)) ? \
  940.     (memcpy(&(p).buf, (p).result, sizeof((p).buf)), (p).result = &(p).buf) : \
  941.     0), \
  942.    (_Xos_processUnlock), \
  943.    (p).result )
  944.  
  945. #elif !defined(_POSIX_THREAD_SAFE_FUNCTIONS) && (defined(__osf__) || defined(hpV4))
  946. /* Returns (int)0 on success.  OSF/1 v3.2, HP/UX 10
  947.  *
  948.  * extern int asctime_r(const struct tm *timeptr, char *buffer, int buflen);
  949.  * extern int ctime_r(const time_t *timer, char *buffer, int buflen);
  950.  * extern int gmtime_r(const time_t *timer, struct tm *result);
  951.  * extern int localtime_r(const time_t *timer, struct tm *result);
  952.  */
  953. # ifdef TIMELEN
  954. typedef char _Xatimeparams[TIMELEN];
  955. typedef char _Xctimeparams[TIMELEN];
  956. # else
  957. typedef char _Xatimeparams[26];
  958. typedef char _Xctimeparams[26];
  959. # endif
  960. typedef struct tm _Xgtimeparams;
  961. typedef struct tm _Xltimeparams;
  962. # define _XAsctime(t,p)        (asctime_r((t),(p),sizeof((p))) ? NULL : (p))
  963. # define _XCtime(t,p)        (ctime_r((t),(p),sizeof((p))) ? NULL : (p))
  964. # define _XGmtime(t,p)        (gmtime_r((t),&(p)) ? NULL : &(p))
  965. # define _XLocaltime(t,p)    (localtime_r((t),&(p)) ? NULL : &(p))
  966.  
  967. #elif !defined(_POSIX_THREAD_SAFE_FUNCTIONS) && defined(sun)
  968. /* Returns NULL on failure.  Solaris 2.5
  969.  *
  970.  * extern char *asctime_r(const struct tm *tm,char *buf, int buflen);
  971.  * extern char *ctime_r(const time_t *clock, char *buf, int buflen);
  972.  * extern struct tm *gmtime_r(const time_t *clock, struct tm *res);
  973.  * extern struct tm *localtime_r(const time_t *clock, struct tm *res);
  974.  */
  975. # ifdef TIMELEN
  976. typedef char _Xatimeparams[TIMELEN];
  977. typedef char _Xctimeparams[TIMELEN];
  978. # else
  979. typedef char _Xatimeparams[26];
  980. typedef char _Xctimeparams[26];
  981. # endif
  982. typedef struct tm _Xgtimeparams;
  983. typedef struct tm _Xltimeparams;
  984. # define _XAsctime(t,p)        asctime_r((t),(p),sizeof((p)))
  985. # define _XCtime(t,p)        ctime_r((t),(p),sizeof((p)))
  986. # define _XGmtime(t,p)        gmtime_r((t),&(p))
  987. # define _XLocaltime(t,p)    localtime_r((t),&(p))
  988.  
  989. #else /* defined(_POSIX_THREAD_SAFE_FUNCTIONS) */
  990. /* POSIX final API.  OSF/1 v4.0, AIX, etc.
  991.  *
  992.  * extern char *asctime_r(const struct tm *timeptr, char *buffer);
  993.  * extern char *ctime_r(const time_t *timer, char *buffer);
  994.  * extern struct tm *gmtime_r(const time_t *timer, struct tm *result);
  995.  * extern struct tm *localtime_r(const time_t *timer, struct tm *result);
  996.  */
  997. # if defined(__osf__)
  998. /* OSF/1 V4.0 <time.h> doesn't declare the _P routines, breaking under C++. */
  999. extern char *_Pasctime_r(const struct tm *, char *);
  1000. extern char *_Pctime_r(const time_t *, char *);
  1001. extern struct tm *_Plocaltime_r(const time_t *, struct tm *);
  1002. # endif
  1003. # ifdef TIMELEN
  1004. typedef char _Xatimeparams[TIMELEN];
  1005. typedef char _Xctimeparams[TIMELEN];
  1006. # else
  1007. typedef char _Xatimeparams[26];
  1008. typedef char _Xctimeparams[26];
  1009. # endif
  1010. typedef struct tm _Xgtimeparams;
  1011. typedef struct tm _Xltimeparams;
  1012. # define _XAsctime(t,p)        asctime_r((t),(p))
  1013. # define _XCtime(t,p)        ctime_r((t),(p))
  1014. # define _XGmtime(t,p)        gmtime_r((t),&(p))
  1015. # define _XLocaltime(t,p)    localtime_r((t),&(p))
  1016. #endif /* X_INCLUDE_TIME_H */
  1017.  
  1018. #if defined(X_INCLUDE_TIME_H) && !defined(_XOS_INCLUDED_TIME_H)
  1019. # define _XOS_INCLUDED_TIME_H
  1020. #endif
  1021.  
  1022.  
  1023. /***** <grp.h> wrappers *****/
  1024.  
  1025. /*
  1026.  * Effective prototypes for <grp.h> wrappers:
  1027.  *
  1028.  * NOTE: On systems lacking appropriate _r functions Getgrgid() and
  1029.  *    Getgrnam() do NOT copy the list of group members!
  1030.  *
  1031.  * Remember that fgetgrent(), setgrent(), getgrent(), and endgrent()
  1032.  * are not included in POSIX.
  1033.  *
  1034.  * #define X_INCLUDE_GRP_H
  1035.  * #define XOS_USE_..._LOCKING
  1036.  * #include <X11/Xos_r.h>
  1037.  *
  1038.  * typedef ... _Xgetgrparams;
  1039.  *
  1040.  * struct group *_XGetgrgid(gid_t, _Xgetgrparams);
  1041.  * struct group *_XGetgrnam(const char *, _Xgetgrparams);
  1042.  */
  1043.  
  1044. #if defined(X_INCLUDE_GRP_H) && !defined(_XOS_INCLUDED_GRP_H)
  1045. # include <grp.h>
  1046. # if defined(XUSE_MTSAFE_API) || defined(XUSE_MTSAFE_GRPAPI)
  1047. #  define XOS_USE_MTSAFE_GRPAPI 1
  1048. # endif
  1049. #endif
  1050.  
  1051. #if !defined(X_INCLUDE_GRP_H) || defined(_XOS_INCLUDED_GRP_H)
  1052. /* Do nothing. */
  1053.  
  1054. #elif !defined(XTHREADS) && !defined(X_FORCE_USE_MTSAFE_API)
  1055. /* Use regular, unsafe API. */
  1056. typedef int _Xgetgrparams;    /* dummy */
  1057. #define _XGetgrgid(g,p)    getgrgid((g))
  1058. #define _XGetgrnam(n,p)    getgrnam((n))
  1059.  
  1060. #elif !defined(XOS_USE_MTSAFE_GRPAPI) || defined(XNO_MTSAFE_GRPAPI)
  1061. /* Systems with thread support but no _r API.  UnixWare 2.0. */
  1062. typedef struct {
  1063.   struct group grp;
  1064.   char buf[X_LINE_MAX];    /* Should be sysconf(_SC_GETGR_R_SIZE_MAX)? */
  1065.   struct group *pgrp;
  1066.   size_t len;
  1067. } _Xgetgrparams;
  1068. #ifdef SVR4
  1069. /* Copy the gr_passwd field too. */
  1070. # define _Xgrp_copyGroup(p) \
  1071.  ( memcpy(&(p).grp, (p).pgrp, sizeof(struct group)), \
  1072.    ((p).grp.gr_name = (p).buf), \
  1073.    ((p).len = strlen((p).pgrp->gr_name)), \
  1074.    strcpy((p).grp.gr_name, (p).pgrp->gr_name), \
  1075.    ((p).grp.gr_passwd = (p).grp.gr_name + (p).len + 1), \
  1076.    ((p).pgrp = &(p).grp), \
  1077.    0 )
  1078. #else
  1079. # define _Xgrp_copyGroup(p) \
  1080.  ( memcpy(&(p).grp, (p).pgrp, sizeof(struct group)), \
  1081.    ((p).grp.gr_name = (p).buf), \
  1082.    strcpy((p).grp.gr_name, (p).pgrp->gr_name), \
  1083.    ((p).pgrp = &(p).grp), \
  1084.    0 )
  1085. #endif
  1086. #define _XGetgrgid(g,p) \
  1087.  ( (_Xos_processLock), \
  1088.    (((p).pgrp = getgrgid((g))) ? _Xgrp_copyGroup(p) : 0), \
  1089.    (_Xos_processUnlock), \
  1090.    (p).pgrp )
  1091. #define _XGetgrnam(n,p) \
  1092.  ( (_Xos_processLock), \
  1093.    (((p).pgrp = getgrnam((n))) ? _Xgrp_copyGroup(p) : 0), \
  1094.    (_Xos_processUnlock), \
  1095.    (p).pgrp )
  1096.  
  1097. #elif !defined(_POSIX_THREAD_SAFE_FUNCTIONS) && (defined(sun) || defined(__osf__))
  1098. /* Non-POSIX API.  Solaris, DEC v3.2.
  1099.  *
  1100.  * extern struct group *getgrgid_r(gid_t, struct group *, char *, int);
  1101.  * extern struct group *getgrnam_r(const char *, struct group *, char *, int);
  1102.  */
  1103. typedef struct {
  1104.   struct group grp;
  1105.   char buf[X_LINE_MAX];    /* Should be sysconf(_SC_GETGR_R_SIZE_MAX)? */
  1106. } _Xgetgrparams;
  1107. #define _XGetgrgid(g,p)    getgrgid_r((g), &(p).grp, (p).buf, sizeof((p).buf))
  1108. #define _XGetgrnam(n,p)    getgrnam_r((n), &(p).grp, (p).buf, sizeof((p).buf))
  1109.  
  1110. #elif !defined(_POSIX_THREAD_SAFE_FUNCTIONS)
  1111. /* Non-POSIX API.  HP/UX 10, AIX 4.
  1112.  *
  1113.  * extern int getgrgid_r(gid_t, struct group *, char *, int);
  1114.  * extern int getgrnam_r(const char *, struct group *, char *, int);
  1115.  */
  1116. typedef struct {
  1117.   struct group grp;
  1118.   char buf[X_LINE_MAX];    /* Should be sysconf(_SC_GETGR_R_SIZE_MAX)? */
  1119. } _Xgetgrparams;
  1120. #define _XGetgrgid(g,p)    \
  1121.  ((getgrgid_r((g), &(p).grp, (p).buf, sizeof((p).buf)) ? NULL : &(p).grp))
  1122. #define _XGetgrnam(n,p)    \
  1123.  ((getgrnam_r((n), &(p).grp, (p).buf, sizeof((p).buf)) ? NULL : &(p).grp))
  1124.  
  1125. #else
  1126. /* POSIX final API.  DEC v4.0, IRIX 6.2.
  1127.  *
  1128.  * int getgrgid_r(gid_t, struct group *, char *, size_t, struct group **);
  1129.  * int getgrnam_r(const char *, struct group *, char *, size_t, struct group **);
  1130.  */
  1131. # if defined(__osf__)
  1132. /* OSF/1 V4.0 <grp.h> doesn't declare the _P routines, breaking under C++. */
  1133. extern int _Pgetgrgid_r(gid_t, struct group *, char *, size_t, struct group **);
  1134. extern int _Pgetgrnam_r(const char *, struct group *, char *, size_t, struct group **);
  1135. # endif
  1136. typedef struct {
  1137.   struct group grp;
  1138.   char buf[X_LINE_MAX];    /* Should be sysconf(_SC_GETGR_R_SIZE_MAX)? */
  1139.   struct group *result;
  1140. } _Xgetgrparams;
  1141.  
  1142. #define _XGetgrgid(g,p)    \
  1143.  ((getgrgid_r((g), &(p).grp, (p).buf, sizeof((p).buf), &(p).result) ? \
  1144.    NULL : (p).result))
  1145. #define _XGetgrnam(n,p)    \
  1146.  ((getgrnam_r((n), &(p).grp, (p).buf, sizeof((p).buf), &(p).result) ? \
  1147.    NULL : (p).result))
  1148. #endif
  1149.  
  1150. #if defined(X_INCLUDE_GRP_H) && !defined(_XOS_INCLUDED_GRP_H)
  1151. # define _XOS_INCLUDED_GRP_H
  1152. #endif
  1153.  
  1154.  
  1155. #ifdef __cplusplus
  1156. }  /* Close scope of 'extern "C"' declaration which encloses file. */
  1157. #endif
  1158.